<?php

namespace App\Models;

use App\Libraries\Common\CommonFun;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Robot extends Model
{
    use HasFactory;



    protected $table = 'robot';

    //查询数据列表
    public static function lists($page = 1, $limit = 20, $params = []){
        $ary_where = array();
        //$ary_where[]=['is_del','=',0];
        $describe=data_get($params,'describe',false);

        if($describe){
            $ary_where[] = ['describe', 'like', '%'.$describe.'%'];
        }

        $query = self::where($ary_where);
        $count = $query->count();
        $data = $query->offset(CommonFun::getOffset($page, $limit))->limit($limit)->orderBy('id', 'desc')->get()->map(function (Robot $rebate){
            $rebate = $rebate->attributesToArray();

           // $rebate['is_status'] = $rebate['status']==1 ?'成功' : '失败';

            return $rebate;
        })->toArray();

        return $data ? compact('count', 'data') : false;
    }

    //对消息进行添加操作
    public static function createApi($data){

        if(! $id=data_get($data,'id',false)){
            $model=new self();
            $model->describe=$data['describe'];
            $model->webhook=$data['webhook'];
            $model->status=$data['status'];

        }else{
            $model = self::where(['id'=>$id])->first();
            $model->describe=$data['describe'];
            $model->webhook=$data['webhook'];
            $model->status=$data['status'];
           // $model->updated_at=date('Y-m-d H:i:s');
        }

        return $model->save() ?true:false;
    }

    /**
     * 格式化时间
     * */
    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
}
